Completed
Push — master ( 9cd6ad...fc3148 )
by Andres
27s
created

upgrades.js ➔ ... ➔ ct.buyUpgrade   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 10
rs 9.4285
nop 3
1
/**
2
 upgrades
3
 Component that handles upgrades.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('upgrades', {
10
  templateUrl: 'views/upgrades.html',
11
  controller: 'ct_upgrades',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_upgrades', ['state', 'visibility', 'upgrade', 'data',
16
function(state, visibility, upgradeService, data) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.upgradeService = upgradeService;
21
  let sortFunc = upgradeService.sortFunctions(data.upgrades);
22
23
  // tries to buy all the upgrades it can, starting from the cheapest
24
  ct.buyAll = function (slot, player) {
25
    let currency = data.elements[slot.element].main;
26
    let cheapest;
27
    let cheapestPrice;
28
    do{
29
      cheapest = null;
30
      cheapestPrice = Infinity;
0 ignored issues
show
Comprehensibility Best Practice introduced by
You seem to be aliasing the built-in name Infinity as cheapestPrice. This makes your code very difficult to follow, consider using the built-in name directly.
Loading history...
31
      for(let up of ct.visibleUpgrades(slot, player)){
32
        let price = data.upgrades[up].price;
33
        if(!slot.upgrades[up] &&
34
          price <= player.resources[currency].number){
35
          if(price < cheapestPrice){
36
            cheapest = up;
37
            cheapestPrice = price;
38
          }
39
        }
40
      }
41
      if(cheapest){
42
        upgradeService.buyUpgrade(player,
43
          slot.upgrades,
44
          data.upgrades[cheapest],
45
          cheapest,
46
          cheapestPrice,
47
          currency);
48
      }
49
    }while(cheapest);
50
  };
51
52
  ct.buyUpgrade = function (name, slot, player) {
53
    let price = data.upgrades[name].price;
54
    let currency = data.elements[slot.element].main;
55
    upgradeService.buyUpgrade(player,
56
      slot.upgrades,
57
      data.upgrades[name],
58
      name,
59
      price,
60
      currency);
61
  };
62
63
  ct.visibleUpgrades = function(slot, player) {
64
    return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[player.options.sortIndex], player);
65
  };
66
67
  function isBasicUpgradeVisible(name, slot, player) {
68
    let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name], player);
69
    return isVisible && (!player.options.hideBought || !slot.upgrades[name]);
70
  }
71
72
  ct.visibleGlobalUpgrades = function() {
73
    return visibility.visible(data.global_upgrades, upgradeService.filterByTag('global'));
74
  };
75
}
76
]);
77